package cn.doitedu.realtime_rule_web.controller;

import cn.doitedu.realtime_rule_web.pojo.*;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSON;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;

@RestController
public class AmisController {
    @RequestMapping("/amis/table/list")
    @ResponseBody
    public AmisVo getPage1Data(Integer page, Integer perPage) {

        Page1Row row1 = new Page1Row("Trident - owy5kg", "Internet Explorer 5.5", "Win 95+", "5.5", "A", "危险", 1);
        Page1Row row2 = new Page1Row("Trident - owy5kg", "Internet Explorer 5.5", "Win 95+", "5.5", "A", "危险", 2);
        Page1Row row3 = new Page1Row("Trident - owy5kg", "Internet Explorer 5.5", "Win 95+", "5.5", "A", "危险", 3);

        ArrayList<Page1Row> rows = new ArrayList<>();
        rows.add(row1);
        rows.add(row2);
        rows.add(row3);

        AmisData amisData = new AmisData(3, rows);
        return new AmisVo(0, "ok", amisData);

    }

    @RequestMapping("/amis/api/getModel/{model_id}")
    @ResponseBody
    public SelectVo getModelList(@PathVariable String model_id) {
        SelectOption m001 = new SelectOption("rule-model-001", "m001");
        SelectOption m002 = new SelectOption("rule-model-002", "m002");
        SelectOption m003 = new SelectOption("rule-model-003", "m003");

        ArrayList<SelectOption> options = new ArrayList<>();
        options.add(m001);
        options.add(m002);
        options.add(m003);

        SelectData selectData = new SelectData(options, model_id);
        return new SelectVo(0, "ok", selectData);
    }

    @RequestMapping("/amis/api/addRule")
    @ResponseBody
    public void addRule(@RequestBody JSONObject jsonObject) {

        System.out.println(jsonObject.toString());

        System.out.println(jsonObject.getString("trig_event_id"));

    }

    @RequestMapping("/amis/api/addRuleRaw")
    @ResponseBody
    public void addRuleRaw(@RequestBody JSONObject jsonObject) {
        System.out.println(jsonObject);

        System.out.println("----------");

        String json = jsonObject.getString("json");

        System.out.println(json);

    }

    @RequestMapping("/amis/api/rule/list")
    @ResponseBody
    public RuleListVo listRules() {

        RuleRow row1 = new RuleRow("r001", "rule-model-001", "雀巢咖啡促销", "2023-02-18 16:00:00", 1200, 40, 1, 1, 42);
        RuleRow row2 = new RuleRow("r002", "rule-model-001", "蒙牛金典促销", "2023-02-12 10:00:00", 4400, 200, 1, 2, 73);
        RuleRow row3 = new RuleRow("r003", "rule-model-002", "伊利会员激活", "2023-02-10 12:00:00", 6600, 60, 1, 3, 15);
        RuleRow row4 = new RuleRow("r004", "rule-model-002", "拉新用户促单", "2023-02-10 12:00:00", 8600, 160, 1, 4, 65);

        RuleData ruleData = new RuleData(3, Arrays.asList(row1, row2, row3, row4));
        return new RuleListVo(0, "ok", ruleData);
    }

    @RequestMapping("/amis/api/ruleModel/list")
    @ResponseBody
    public RuleModelListVo listModels() {
        RuleModelRow row1 = new RuleModelRow("rule-model-001", "事件次数类模型", "2023-02-01");
        RuleModelRow row2 = new RuleModelRow("rule-model-002", "事件序列类模型", "2023-02-11");
        RuleModelRow row3 = new RuleModelRow("rule-model-003", "属性SUM计算模型", "2023-03-08");

        RuleModelData ruleModelData = new RuleModelData(Arrays.asList(row1, row2, row3));
        return new RuleModelListVo(0, "ok", ruleModelData);

    }

    @RequestMapping("/amis/api/crud/{id}")
    public AmisVo getTableRowById(@PathVariable int id) {
        Page1Row row1 = new Page1Row("Trident - owy5kg", "Internet Explorer 5.5", "Win 95+", "5.5", "A", "危险", 1);

        return new AmisVo(0, "ok", row1);
    }

    // {"status":0,"msg":"ok","data":{"line":[54,99,34,52,18,21]}}
    @RequestMapping("/amis/api/chart/chart2")
    public JSONObject getChartDate() {
        String json = "{\"status\":0,\"msg\":\"ok\",\"data\":{\"title\":{\"text\":\"今日触发趋势\",\"subtext\":\"纯属虚构\"},\"tooltip\":{\"trigger\":\"axis\",\"axisPointer\":{\"type\":\"cross\"}},\"toolbox\":{\"show\":true,\"feature\":{\"saveAsImage\":{}}},\"xAxis\":{\"type\":\"category\",\"boundaryGap\":false,\"data\":[\"00:00\",\"01:15\",\"02:30\",\"03:45\",\"05:00\",\"06:15\",\"07:30\",\"08:45\",\"10:00\",\"11:15\",\"12:30\",\"13:45\",\"15:00\",\"16:15\",\"17:30\",\"18:45\",\"20:00\",\"21:15\",\"22:30\",\"23:45\"]},\"yAxis\":{\"type\":\"value\",\"axisLabel\":{\"formatter\":\"{value} W\"},\"axisPointer\":{\"snap\":true}},\"visualMap\":{\"show\":false,\"dimension\":0,\"pieces\":[{\"lte\":6,\"color\":\"green\"},{\"gt\":6,\"lte\":8,\"color\":\"red\"},{\"gt\":8,\"lte\":14,\"color\":\"green\"},{\"gt\":14,\"lte\":17,\"color\":\"red\"},{\"gt\":17,\"color\":\"green\"}]},\"series\":[{\"name\":\"用电量\",\"type\":\"line\",\"smooth\":true,\"data\":[149,191,72,9,186,67,186,123,106,85,12,154,108,96,44,68,74,32,68,175,117,177,118,2],\"markArea\":{\"data\":[[{\"name\":\"早高峰\",\"xAxis\":\"07:30\"},{\"xAxis\":\"10:00\"}],[{\"name\":\"晚高峰\",\"xAxis\":\"17:30\"},{\"xAxis\":\"21:15\"}]]}}]}}";

        JSONObject jsb = new JSONObject();
        jsb.put("status", 0);
        jsb.put("msg", "ok");


        JSONArray xLineJson = new JSONArray(Arrays.asList("00:00", "01:15", "02:30", "03:45", "05:00", "06:15", "07:30", "08:45", "10:00", "11:15", "12:30", "13:45", "15:00", "16:15", "17:30", "18:45", "20:00", "21:15", "22:30", "23:45"));
        JSONArray yLineJson = new JSONArray(Arrays.asList(143, 53, 141, 20 + RandomUtils.nextInt(40), 178, 113, 6, 105, 169, 50 + RandomUtils.nextInt(200), 183, 80, 120, 145, 35, 10 + RandomUtils.nextInt(100), 77, 89, 52, 131, 81, 15, 160 + RandomUtils.nextInt(20), 31));

        JSONObject dataJsb = new JSONObject();
        dataJsb.put("xLine", xLineJson);
        dataJsb.put("yLine", yLineJson);

        jsb.put("data", dataJsb);

        return jsb;
    }

    @RequestMapping("/amis/api/chart/chart")
    public JSONObject getChart() {

        // [5,49,7,74,26,62]
        String line = "[" + RandomUtils.nextInt(70) + ","
                + RandomUtils.nextInt(70) + ","
                + RandomUtils.nextInt(70) + ","
                + RandomUtils.nextInt(70) + ","
                + RandomUtils.nextInt(70) + ","
                + RandomUtils.nextInt(70) + "]";


        String json = "{\"status\":0,\"msg\":\"ok\",\"data\":{\"title\":{\"text\":\"每日触发量\"},\"tooltip\":{},\"legend\":{\"data\":[\"触发量\"]},\"xAxis\":{\"data\":[\"2023-02-15\",\"2023-02-16\",\"2023-02-17\",\"2023-02-18\",\"2023-02-19\",\"2023-02-20\"]},\"yAxis\":{},\"series\":[{\"name\":\"触发量\",\"type\":\"bar\",\"data\":" + line + "}]}}";
        return JSON.parseObject(json);
    }

    @RequestMapping("/amis/api/meta/eventProperty")
    public SelectVo getEventProperties(String eventId) {

        ArrayList<SelectOption> lst = new ArrayList<>();

        if (StringUtils.isBlank(eventId)) eventId = "";

        lst.add(new SelectOption(eventId + "_p1", eventId + "_p1"));
        lst.add(new SelectOption(eventId + "_p2", eventId + "_p2"));
        lst.add(new SelectOption(eventId + "_p3", eventId + "_p3"));

        return new SelectVo(0, "ok", new SelectData(lst, eventId + "_p1"));

    }

    @RequestMapping("/amis/api/meta/eventIds")
    public SelectVo getEventIds() {
        ArrayList<SelectOption> lst = new ArrayList<>();
        lst.add(new SelectOption("page_view", "0"));
        lst.add(new SelectOption("add_cart", "1"));
        lst.add(new SelectOption("search", "2"));
        lst.add(new SelectOption("item_share", "3"));
        lst.add(new SelectOption("ad_show", "4"));
        lst.add(new SelectOption("ad_click", "5"));

        return new SelectVo(0, "ok", new SelectData(lst, ""));

    }

}
